﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using RescueApp.Model;

namespace RescueApp.Data.Config
{
    public class RescueDispatchConfig : IEntityTypeConfiguration<RescueDispatch>
    {
        public void Configure(EntityTypeBuilder<RescueDispatch> builder)
        {
            builder.ToTable("RescueDispatch");

            builder.HasKey(r => r.DispatchID);
            builder.Property(r => r.DispatchID).ValueGeneratedOnAdd();

            builder.Property(r => r.RescueID).IsRequired();
            builder.Property(r => r.Driver).IsRequired().HasMaxLength(20);
            builder.Property(r => r.LicenseID).IsRequired().HasMaxLength(10);
            builder.Property(r => r.ArrivedTime).IsRequired();
            builder.Property(r => r.Cause).IsRequired().HasMaxLength(200);
            builder.Property(r => r.Cost).IsRequired().HasColumnType("decimal(18,2)");

            // 配置外键关系
            builder.HasOne(r => r.RescueApply) // 指定 RescueApply 是关联的实体
                .WithMany() // RescueApply 可以有多个 RescueDispatch
                .HasForeignKey(r => r.RescueID) // 指定 RescueID 是外键
                .OnDelete(DeleteBehavior.Restrict); // 删除行为：限制删除关联的 RescueApply
        }
    }
}